﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VehicleConfigurator.NetUtils.Database;
using System.Collections;

namespace VehicleConfigurator.FormDetails
{
    public partial class FrmDuplicatePricelist : Form
    {
        public FrmDuplicatePricelist()
        {
            InitializeComponent();
        }

        private object countryOid = string.Empty;
        private object currencyOid = string.Empty;
        private object modelYearOid = string.Empty;
        private object pricelistDate = string.Empty;

        public void LoadDataComponent(DataTable country, DataTable currency, DataTable modelYear)
        {
            // Country
            this.cboCountry.DataSource = country;
            this.cboCountry.ValueMember = FieldNames.OID;
            this.cboCountry.DisplayMember = FieldNames.NAME;
            // Currency
            this.cboCurrency.DataSource = currency;
            this.cboCurrency.ValueMember = FieldNames.OID;
            this.cboCurrency.DisplayMember = FieldNames.NAME;
            // Model year
            this.cboModelYear.DataSource = modelYear;
            this.cboModelYear.ValueMember = FieldNames.OID;
            this.cboModelYear.DisplayMember = FieldNames.NAME;
        }

        public void LoadDataDefault(int countryOid, int currencyOid, int modelYearOid, DateTime pricelistDate)
        {
            try
            {
                this.countryOid = countryOid;
                this.currencyOid = currencyOid;
                this.modelYearOid = modelYearOid;
                this.pricelistDate = pricelistDate;
                this.cboCountry.SelectedValue = countryOid;
                this.cboCurrency.SelectedValue = currencyOid;
                this.cboModelYear.SelectedValue = modelYearOid;
                // Pricelist Date
                this.dtPricelistDate.Value = pricelistDate;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                var countryOid = this.cboCountry.SelectedValue;
                var currencyOid = this.cboCurrency.SelectedValue;
                var modelYearOid = this.cboModelYear.SelectedValue;
                var pricelistDate = this.dtPricelistDate.Value;
                if (countryOid == null)
                {
                    throw new Exception("Please select a Country.");
                }
                else if (currencyOid == null)
                {
                    throw new Exception("Please select a Currency.");
                }
                else if (modelYearOid == null)
                {
                    throw new Exception("Please select a Model year.");
                }
                else if (this.countryOid.Equals(countryOid)
                    && this.currencyOid.Equals(currencyOid)
                    && this.modelYearOid.Equals(modelYearOid)
                    && this.pricelistDate.Equals(pricelistDate))
                {
                    throw new Exception("Please change selection");
                }
                DialogResult result1 = MessageBox.Show("Are you sure?", "Confirm Message", MessageBoxButtons.YesNo);
                if (result1 == DialogResult.Yes)
                {
                    var props = new Hashtable();
                    //props[FieldNames.OID] = this.rowSelectedDuplicate;
                    props[FieldNames.OID_COUNTRY] = countryOid;
                    props[FieldNames.OID_CURRENCY] = currencyOid;
                    props[FieldNames.OID_MODEL_YEAR] = modelYearOid;
                    props[FieldNames.PRICE_LIST_DATE] = pricelistDate;

                    //props[FieldNames.OPTION_GROUP_PRICELIST] = this.groupOid;
                    //props[FieldNames.OPTION_OID] = this.optionOid;

                    this.Tag = props;
                    this.DialogResult = DialogResult.OK;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
